#include "lolevel.h"
#include "platform.h"
#include "core.h"

static long *nrflag = (long*)0x9F90;

#include "../../../generic/capt_seq.c"

void __attribute__((naked,noinline)) capt_seq_task() {
	
	asm volatile (

"                STMFD   SP!, {R3-R7,LR}\n"
"                LDR     R6, =0x5170\n"
"loc_FFC4AE8C:\n"
"                LDR     R0, [R6,#8]\n"
"                MOV     R2, #0\n"
"                MOV     R1, SP\n"
"                BL      sub_FFC1693C\n"			// _sub_FFC1693C__KerQueue_c__0 ; LOCATION: KerQueue.c:0
"                TST     R0, #1\n"
"                BEQ     loc_FFC4AEB8\n"
"                LDR     R1, =0x539\n"
"                LDR     R0, =0xFFC4AA98\n"			// "SsShootTask.c"
"                BL      sub_FFC0BD98\n"				// DebugAssert
"                BL      sub_FFC0BB50\n"				// eventproc_export_ExitTask ; LOCATION: KerTask.c:0
"                LDMFD   SP!, {R3-R7,PC}\n"
"loc_FFC4AEB8:\n"
"                LDR     R0, [SP]\n"
"                LDR     R1, [R0]\n"
"                CMP     R1, #0x1D\n"
"                ADDLS   PC, PC, R1,LSL#2\n"
"                B       loc_FFC4B084\n"
"loc_FFC4AECC:\n"
"                B       loc_FFC4AF44\n"
"loc_FFC4AED0:\n"
"                B       loc_FFC4AF4C\n"
"loc_FFC4AED4:\n"
"                B       loc_FFC4AF54\n"
"loc_FFC4AED8:\n"
"                B       loc_FFC4AF68\n"
"loc_FFC4AEDC:\n"
"                B       loc_FFC4AF60\n"
"loc_FFC4AEE0:\n"
"                B       loc_FFC4AF70\n"
"loc_FFC4AEE4:\n"
"                B       loc_FFC4AF78\n"
"loc_FFC4AEE8:\n"
"                B       loc_FFC4AF84\n"
"loc_FFC4AEEC:\n"
"                B       loc_FFC4AFDC\n"
"loc_FFC4AEF0:\n"
"                B       loc_FFC4AF68\n"
"loc_FFC4AEF4:\n"
"                B       loc_FFC4AFE4\n"
"loc_FFC4AEF8:\n"
"                B       loc_FFC4AFF4\n"
"loc_FFC4AEFC:\n"
"                B       loc_FFC4AFFC\n"
"loc_FFC4AF00:\n"
"                B       loc_FFC4B004\n"
"loc_FFC4AF04:\n"
"                B       loc_FFC4B00C\n"
"loc_FFC4AF08:\n"
"                B       loc_FFC4B014\n"
"loc_FFC4AF0C:\n"
"                B       loc_FFC4B01C\n"
"loc_FFC4AF10:\n"
"                B       loc_FFC4B024\n"
"loc_FFC4AF14:\n"
"                B       loc_FFC4B030\n"
"loc_FFC4AF18:\n"
"                B       loc_FFC4B038\n"
"loc_FFC4AF1C:\n"
"                B       loc_FFC4B040\n"
"loc_FFC4AF20:\n"
"                B       loc_FFC4B048\n"
"loc_FFC4AF24:\n"
"                B       loc_FFC4B050\n"
"loc_FFC4AF28:\n"
"                B       loc_FFC4B05C\n"
"loc_FFC4AF2C:\n"
"                B       loc_FFC4B064\n"
"loc_FFC4AF30:\n"
"                B       loc_FFC4B06C\n"
"loc_FFC4AF34:\n"
"                B       loc_FFC4B074\n"
"loc_FFC4AF38:\n"
"                B       loc_FFC4B07C\n"
"loc_FFC4AF3C:\n"
"                B       loc_FFC4B090\n"
"loc_FFC4AF40:\n"
"                B       loc_FFC4B090\n"
"loc_FFC4AF44:\n"
"                BL      sub_FFC4B710\n"  		// _sub_FFC4B710__SsPrepareSeq_c__0
"                BL      shooting_expo_param_override\n"
"                B       loc_FFC4AF7C\n"
"loc_FFC4AF4C:\n"
"                BL      sub_FFC4B1AC_my\n"			 // --> changed
"                B       loc_FFC4B090\n"
"loc_FFC4AF54:\n"
"                MOV     R0, #1\n"
"                BL      sub_FFC4B8AC\n"			// _sub_FFC4B8AC__SsPrepareSeq_c__0
"                B       loc_FFC4B090\n"
"loc_FFC4AF60:\n"
"                BL      sub_FFC4B390\n"
"                B       loc_FFC4B090\n"
"loc_FFC4AF68:\n"
"                BL      sub_FFC4B6F0\n"
"                B       loc_FFC4B090\n"
"loc_FFC4AF70:\n"
"                BL      sub_FFC4B6F8\n"
"                B       loc_FFC4B090\n"
"loc_FFC4AF78:\n"
"                BL      sub_FFC4B7CC\n"			//	_sub_FFC4B7CC__SsPrepareSeq_c__0
"loc_FFC4AF7C:\n"
"                BL      sub_FFC491F4\n"
"                B       loc_FFC4B090\n"
"loc_FFC4AF84:\n"
"                LDR     R4, [R0,#0xC]\n"
"                BL      sub_FFC4B700\n"
"                MOV     R0, R4\n"
"                BL      sub_FFD0B4C8\n"			// _sub_FFD0B4C8__SsPrePreSeq_c__2097152
"                TST     R0, #1\n"
"                MOV     R5, R0\n"
"                BNE     loc_FFC4AFC4\n"
"                BL      sub_FFC5923C\n"			//	_sub_FFC5923C__PropertyCase_c__0
"                STR     R0, [R4,#0x18]\n"
"                MOV     R0, R4\n"
"                BL      sub_FFD0C2B8\n"			// _sub_FFD0C2B8__SsCaptureSeq_c__832
"                MOV     R0, R4\n"
"                BL      sub_FFD0C76C\n"			// _sub_FFD0C76C__SsCaptureSeq_c__4
"                MOV     R5, R0\n"
"                LDR     R0, [R4,#0x18]\n"
"                BL      sub_FFC59450\n"			// _sub_FFC59450__PropertyCase_c__0
"loc_FFC4AFC4:\n"
"                BL      sub_FFC4B6F0\n"
"                MOV     R2, R4\n"
"                MOV     R1, #9\n"
"                MOV     R0, R5\n"
"                BL      sub_FFC495D0\n"			// _sub_FFC495D0__SsShootCtrl_c__0
"                B       loc_FFC4B090\n"
"loc_FFC4AFDC:\n"
"                BL      sub_FFC4B82C\n"			// _sub_FFC4B82C__SsPrepareSeq_c__0
"                B       loc_FFC4AF7C\n"
"loc_FFC4AFE4:\n"
"                LDR     R0, =0x18CB0\n"
"                LDR     R0, [R0,#0x4C]\n"
"                BL      sub_FFC4BBC4\n"			// _sub_FFC4BBC4__SsMovieRec_c__328
//"                BL      sub_FFC4BBC4_my\n"		// This was for movie record. Temporarly commented
"                B       loc_FFC4B090\n"
"loc_FFC4AFF4:\n"
"                BL      sub_FFC4BE6C\n"			// _sub_FFC4BE6C__SsMovieRec_c__504
"                B       loc_FFC4B090\n"
"loc_FFC4AFFC:\n"
"                BL      sub_FFC4BEF8\n"			// _sub_FFC4BEF8__SsMovieRec_c__43
"                B       loc_FFC4B090\n"
"loc_FFC4B004:\n"
"                BL      sub_FFD0B6EC\n"
"                B       loc_FFC4B090\n"
"loc_FFC4B00C:\n"
"                BL      sub_FFD0B8D4\n"
"                B       loc_FFC4B090\n"
"loc_FFC4B014:\n"
"                BL      sub_FFD0B964\n"			// _sub_FFD0B964__SsExpCompSeq_c__105
"                B       loc_FFC4B090\n"
"loc_FFC4B01C:\n"
"                BL      sub_FFD0BA0C\n"
"                B       loc_FFC4B090\n"
"loc_FFC4B024:\n"
"                MOV     R0, #0\n"
"                BL      sub_FFD0BBB0\n"
"                B       loc_FFC4B090\n"
"loc_FFC4B030:\n"
"                BL      sub_FFD0BCF0\n"
"                B       loc_FFC4B090\n"
"loc_FFC4B038:\n"
"                BL      sub_FFD0BD84\n"			// _sub_FFD0BD84__SsMFSeq_c__114
"                B       loc_FFC4B090\n"
"loc_FFC4B040:\n"
"                BL      sub_FFD0BE48\n"			// _sub_FFD0BE48__SsMFSeq_c__189
"                B       loc_FFC4B090\n"
"loc_FFC4B048:\n"
"                BL      sub_FFC4BA14\n"
"                B       loc_FFC4B090\n"
"loc_FFC4B050:\n"
"                BL      sub_FFC4BA40\n"
"                BL      sub_FFC14170\n"
"                B       loc_FFC4B090\n"
"loc_FFC4B05C:\n"
"                BL      sub_FFD0BAC8\n"
"                B       loc_FFC4B090\n"
"loc_FFC4B064:\n"
"                BL      sub_FFD0BB0C\n"			// _sub_FFD0BB0C__SsChgExpSeq_c__0
"                B       loc_FFC4B090\n"
"loc_FFC4B06C:\n"
"                BL      sub_FFC4D3A4\n"
"                B       loc_FFC4B090\n"
"loc_FFC4B074:\n"
"                BL      sub_FFC4D3C0\n"
"                B       loc_FFC4B090\n"
"loc_FFC4B07C:\n"
"                BL      sub_FFC4D3D0\n"
"                B       loc_FFC4B090\n"
"loc_FFC4B084:\n"
"                LDR     R1, =0x65E\n"
"                LDR     R0, =0xFFC4AA98\n"
"                BL      sub_FFC0BD98\n"
"loc_FFC4B090:\n"
"                LDR     R0, [SP]\n"
"                LDR     R1, [R0,#4]\n"
"                LDR     R0, [R6,#4]\n"
"                BL      sub_FFC166AC\n"
"                LDR     R4, [SP]\n"
"                LDR     R0, [R4,#8]\n"
"                CMP     R0, #0\n"
"                LDREQ   R1, =0x11D\n"
"                LDREQ   R0, =0xFFC4AA98\n"
"                BLEQ    sub_FFC0BD98\n"
"                MOV     R0, #0\n"
"                STR     R0, [R4,#8]\n"
"                B       loc_FFC4AE8C\n"
	);
}

/* OK */
void __attribute((naked,noinline)) sub_FFC4B1AC_my() {
     asm volatile (
		     "STMFD   SP!, {R3-R5,LR}\n"
		     "LDR     R4, [R0,#0xC]\n"
		     "LDR     R0, [R4,#8]\n"
		     "ORR     R0, R0, #1\n"
		     "STR     R0, [R4,#8]\n"
		     "MOV     R0, #2\n"
		     "BL      sub_FFC45F20\n"
		     "BL      sub_FFC4B700\n"
		     "MOV     R0, R4\n"
		     "BL      sub_FFC4B9C8\n"
		     "MOV     R0, R4\n"
		     "BL      sub_FFD0B150\n"
		     "CMP     R0, #0\n"
		     "MOV     R0, R4\n"
		     "BEQ     loc_FFC4B204\n"
		     "BL      sub_FFD0B1EC\n"
		     "TST     R0, #1\n"
		     "MOVNE   R2, R4\n"
		     "LDMNEFD SP!, {R3-R5,LR}\n"
		     "MOVNE   R1, #1\n"
		     "BNE     sub_FFC495D0\n"
		     "B       loc_FFC4B208\n"
		     
	"loc_FFC4B204:\n"
		     "BL      sub_FFD0B1A0\n"
		     
	"loc_FFC4B208:\n"
		     "MOV     R0, #0\n"
		     "STR     R0, [SP]\n"
		     "LDR     R0, =0x18CB0\n"
		     "MOV     R2, #2\n"
		     "LDRH    R0, [R0,#0x8A]\n"
		     "MOV     R1, SP\n"
		     "CMP     R0, #3\n"
		     "LDRNE   R0, [R4,#0xC]\n"
		     "CMPNE   R0, #1\n"
		     "MOVHI   R0, #1\n"
		     "STRHI   R0, [SP]\n"
		     "LDR     R0, =0x123\n"
		     "BL      sub_FFC58FA4\n"				// PT_GetPropertyCaseString
		     "BL      sub_FFD2D9A0\n"
		     "BL      sub_FFC5923C\n"				// _sub_FFC5923C__PropertyCase_c__0
		     "STR     R0, [R4,#0x18]\n"
		     "MOV     R0, R4\n"
		     "BL      sub_FFD0C2B8\n"		// _sub_FFD0C2B8__SsCaptureSeq_c__832
		     "BL      sub_FFD0CE08\n"		// _sub_FFD0CE08__SsShootLib_c__32768
		     "MOV     R0, R4\n"
		     "BL      sub_FFD0C380_my\n"				// --> NR Override here (_sub_FFD0C380__SsCaptureSeq_c__860)
		     "MOV     R5, R0\n"
		     "BL      capt_seq_hook_raw_here\n" // +-----------> Capture RAW, camera already did badpixel-sub (always) though some pixels are still there
		     "BL      sub_FFC4D3C0\n"
		     "BL      sub_FFC4D3FC\n"
		     "MOV     R2, R4\n"
		     "MOV     R1, #1\n"
		     "MOV     R0, R5\n"
		     "BL      sub_FFC495D0\n"
		     "BL      sub_FFD0C6FC\n"
		     "CMP     R0, #0\n"
		     "LDRNE   R0, [R4,#8]\n"
		     "ORRNE   R0, R0, #0x2000\n"
		     "STRNE   R0, [R4,#8]\n"
		     "LDMFD   SP!, {R3-R5,PC}\n"
		     "\n"
     );
}


/* OK */
void __attribute__((naked,noinline)) sub_FFD0C380_my() {
 asm volatile(
		     "STMFD   SP!, {R1-R9,LR}\n"
		     "MOV     R4, R0\n"
		     "BL      sub_FFD0CF68\n"			// _sub_FFD0CF68__SsShootEvent_c__60
		     "MOV    	R1, #-1\n"
		     "BL      sub_FFC166E0\n"
		     "MOV     R2, #4\n"
		     "ADD     R1, SP, #0x28-0x24\n"
		     "MOV     R0, #0x8A\n"
		     "BL      sub_FFC590AC\n"		// PT_GetPropertyCaseString_0
		     "TST     R0, #1\n"
		     "MOVNE   R1, #0x35C\n"
		     "LDRNE   R0, =0xFFD0C4F8\n"		// "SsCaptureSeq.c"
		     "BLNE    sub_FFC0BD98\n"		// DebugAssert
		     "LDR     R7, =0x18D68\n"
		     "LDR     R6, =0x18CB0\n"
		     "LDRSH   R1, [R7,#0xE]\n"
		     "LDR     R0, [R6,#0x80]\n"
		     "BL      sub_FFCD68B4\n"
		     "BL      sub_FFC33494\n"		// GetCCDTemperature
		     "LDR     R3, =0x8590\n"
		     "STRH    R0, [R4,#0x94]\n"
		     "STR    R3, [SP]\n"     // for the final until everyone uses 'new' gcc\n"
		     "MOV     R1, R0\n"
		     "LDRH    R0, [R6,#0x54]\n"
		     "LDRSH   R2, [R7,#0xC]\n"
		     "SUB     R3, R3, #4\n"
		     "BL      sub_FFD0D560\n"		// _sub_FFD0D560__NRTable_c__219
		     "BL      wait_until_remote_button_is_released\n"
		     "BL      capt_seq_hook_set_nr\n" // +-----------> so immediately override NR
		     "B 		  sub_FFD0C3EC\n"         // continue function in firmware (from sx110)
		     );
}

